// dao/assetPaths.js
import { connect } from '../db';
import os from 'os';
import path from 'path';
import process from 'process';
import { reloadConfig, serviceUrl } from '../config/config'

const isWin = process.platform === 'win32';

// 获取所有资源路径
export function getAllAssetPaths() {
  const db = connect();
  return db.prepare('SELECT * FROM asset_paths ORDER BY id ASC').all();
}

// 添加新资源路径
export function addAssetPath(pathKey, pathValue, description = '', enabled = 1) {
  const db = connect();
  const now = Date.now();
  const info = db.prepare(
    'INSERT INTO asset_paths (path_key, path_value, description, enabled, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)'
  ).run(pathKey, pathValue, description, enabled, now, now);
  reloadConfig()
  return info;
}

// 更新资源路径
export function updateAssetPath(id, { pathKey, pathValue, description, enabled }) {
  const db = connect();
  const now = Date.now();
  const info = db.prepare(
    'UPDATE asset_paths SET path_key = ?, path_value = ?, description = ?, enabled = ?, updated_at = ? WHERE id = ?'
  ).run(pathKey, pathValue, description, enabled, now, id);
  reloadConfig()
  return info;
}

// 根据ID删除资源路径
export function deleteAssetPath(id) {
  const db = connect();
  const info = db.prepare('DELETE FROM asset_paths WHERE id = ?').run(id);
  reloadConfig()
  return info;
}

// 启用/禁用资源路径
export function toggleAssetPathStatus(id, enabled) {
  const db = connect();
  const now = Date.now();
  const info = db.prepare(
    'UPDATE asset_paths SET enabled = ?, updated_at = ? WHERE id = ?'
  ).run(enabled ? 1 : 0, now, id);
  reloadConfig()
  return info;
}

// 根据ID获取单个资源路径
export function getAssetPathById(id) {
  const db = connect();
  return db.prepare('SELECT * FROM asset_paths WHERE id = ?').get(id);
}

// 获取baseUrl
export function getBaseUrl() {
  return serviceUrl.kpopBaseUrl;
}

// 根据键获取单个资源路径
export function getAssetPathByKey(pathKey) {
  const db = connect();
  return db.prepare('SELECT * FROM asset_paths WHERE path_key = ? AND enabled = 1').get(pathKey);
}

// 获取所有启用的资源路径配置
export function getAllEnabledAssetPaths() {
  const db = connect();
  return db.prepare('SELECT * FROM asset_paths WHERE enabled = 1').all();
}

// 获取默认资源路径配置
export function getDefaultAssetPaths() {
  return {
    model: isWin
      ? path.join('\\\\Fw\\d', 'heygem_data', 'face2face', 'temp')
      : path.join(os.homedir(), 'heygem_data', 'face2face', 'temp'),
    ttsProduct: isWin
      ? path.join('\\\\Fw\\d', 'heygem_data', 'face2face', 'temp')
      : path.join(os.homedir(), 'heygem_data', 'face2face', 'temp'),
    ttsRoot: isWin
      ? path.join('\\\\Fw\\d', 'heygem_data', 'voice', 'data')
      : path.join(os.homedir(), 'heygem_data', 'voice', 'data'),
    ttsTrain: isWin
      ? path.join('\\\\Fw\\d', 'heygem_data', 'voice', 'data', 'origin_audio')
      : path.join(os.homedir(), 'heygem_data', 'voice', 'data', 'origin_audio')
  };
}

// 获取当前生效的资源路径配置(结合数据库配置和默认配置)
export async function getEffectiveAssetPaths() {
  const defaultPaths = getDefaultAssetPaths();
  const dbPaths = {};

  try {
    const enabledPaths = getAllEnabledAssetPaths();

    // 将数据库中的配置转换为键值对
    enabledPaths.forEach(item => {
      dbPaths[item.path_key] = item.path_value;
    });

    // 合并默认配置和数据库配置，数据库配置优先
    return {
      ...defaultPaths,
      ...dbPaths
    };
  } catch (error) {
    console.error('获取资源路径配置失败，使用默认配置:', error);
    return defaultPaths;
  }
}
